import db from 'utils/localstorage'
import vue from 'vue'
import { loadLanguageAsync } from '@/lang'

export default {
  namespaced: true,
  state: {
    token: db.get('USER_TOKEN'),
    expireTime: db.get('EXPIRE_TIME'),
    user: db.get('USER'),
    lang: 'zh-CN'
  },
  mutations: {
    UPDATE_DATA: (state, payload) => {
      for (let key in payload) {
        vue.set(state, key, payload[key])
      }
    },
    setToken (state, val) {
      db.save('USER_TOKEN', val)
      state.token = val
    },
    setExpireTime (state, val) {
      db.save('EXPIRE_TIME', val)
      state.expireTime = val
    },
    setUser (state, val) {
      db.save('USER', val)
      state.user = val
    },
    setLang (state, val) {
      db.save('LANG', val)
      state.lang = val
    }
  },
  actions: {
    setData ({ commit }, payload) {
      commit('UPDATE_DATA', payload)
    },
    setLang ({ commit }, lang) {
      commit('setLang', lang)
      console.log(lang)
      loadLanguageAsync(lang)
      window.location.reload()
    }
  }
}
